package org.grow.swimming;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import javax.sql.DataSource;

@SpringBootApplication
public class SwimmingApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwimmingApplication.class, args);
    }

    @Value("classpath:init.sql")
    private Resource businessScript;

    /**
     * 自定义Bean实现业务的特殊需求
     *
     * @param dataSource
     * @return
     */
//    @Bean
    public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        // 设置数据源
        initializer.setDataSource(dataSource);
        final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.setSqlScriptEncoding("utf8");
        populator.addScripts(businessScript);
        initializer.setDatabasePopulator(populator);
        return initializer;
    }


}
